package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.ActivityStoreDAO;
import com.ecoolex.hotel.saas.entity.ActivityStore;
import com.ecoolex.hotel.saas.entity.ActivityStoreExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class ActivityStoreManager {

    @Autowired
    private ActivityStoreDAO dao;

    public ActivityStore getActivityStore(Integer id) {
        return dao.selectByPrimaryKeyWithLogicalDelete(id, ActivityStore.NOT_DELETED);
    }

    public Pagination<ActivityStore> queryActivityStore(String storeName, Integer activityId, Page page) {
        return Pagination.create(page, dao.count(storeName, activityId), () -> dao.query(storeName, activityId, page));
    }

    public List<ActivityStore> queryActivityStore(Integer activityId, Integer size) {
        ActivityStoreExample example = new ActivityStoreExample();
        ActivityStoreExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andDeletedEqualTo(ActivityStore.NOT_DELETED);
        example.orderBy(ActivityStore.Column.weight.desc());
        example.limit(size);
        return dao.selectByExample(example);
    }

    public void addActivityStore(Integer activityId, List<Integer> storeIdList, String createUser) {
        for (Integer storeId : storeIdList) {
            if (isRelationActivity(activityId, storeId)) {
                continue;
            }
            ActivityStore record = new ActivityStore();
            record.setActivityId(activityId);
            record.setStoreId(storeId);
            record.setDeleted(ActivityStore.NOT_DELETED);
            record.setCreateUser(createUser);
            record.setCreateTime(Date.from(Instant.now()));
            record.setUpdateUser(createUser);
            record.setUpdateTime(Date.from(Instant.now()));
            dao.insert(record);
        }
    }

    public ActivityStore editActivityStore(ActivityStore record, String updateUser) {
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, ActivityStore.Column.weight, ActivityStore.Column.updateUser, ActivityStore.Column.updateTime);
        return record;
    }

    public List<ActivityStore> queryByActivityId(Integer activityId) {
        ActivityStoreExample example = new ActivityStoreExample();
        ActivityStoreExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andDeletedEqualTo(ActivityStore.NOT_DELETED);
        return dao.selectByExample(example);
    }

    public void deleteByActivityId(Integer activityId, String updateUser) {
        List<ActivityStore> list = queryByActivityId(activityId);
        list.forEach(activityStore -> {
            delete(activityStore, updateUser);
        });
    }

    public void delete(ActivityStore record, String updateUser) {
        record.setDeleted(ActivityStore.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, ActivityStore.Column.deleted, ActivityStore.Column.updateUser, ActivityStore.Column.updateTime);
    }

    public Boolean isRelationActivity(Integer activityId, Integer storeId) {
        ActivityStoreExample example = new ActivityStoreExample();
        ActivityStoreExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andStoreIdEqualTo(storeId).andDeletedEqualTo(ActivityStore.NOT_DELETED);
        return Check.notNull(dao.selectOneByExample(example));
    }
}